//
//  XGPictureTopicView.m
//  BSBuDeJie
//
//  Created by monkey on 2019/1/1.
//  Copyright © 2019 itcast. All rights reserved.
//

#import <Masonry/Masonry.h>

#import "UIImageView+Extension.h"
#import "UIImage+EXtension.h"

#import "XGPictureTopicView.h"

#import "XGTopicModel.h"

#import "XGSeePictureViewController.h"


@interface XGPictureTopicView ()

/// 图片
@property (nonatomic,strong) UIImageView *imageView;
/// 是否是gif
@property (nonatomic,strong) UIImageView *isGifImageView;
/// 点击查看大图按钮
@property (nonatomic,strong) UIButton *seeBigPictureButton;

@end

@implementation XGPictureTopicView

#pragma mark - 解析模型

- (void)setTopicModel:(XGTopicModel *)topicModel
{
    _topicModel = topicModel;
    
    self.isGifImageView.hidden = !topicModel.isGif;
    self.seeBigPictureButton.hidden = !topicModel.isBigImage;
    
    [self.imageView xg_setImageWithURLString:topicModel.imageURL placeHolder:kPlaceHolderImage completion:^(NSError *error, UIImage *image) {
        if (error) {
            return;
        }
       
        [image scaleToWidth:SCREEN_WIDTH - 2 * kMargin completion:^(UIImage * _Nonnull image) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.imageView.image = image;
            });
        }];
    }];
}

#pragma mark - 事件监听

- (void)tapImageViewAction:(UIGestureRecognizer *)tap
{
    XGSeePictureViewController *viewController = [XGSeePictureViewController viewControllerWithTopicModel:self.topicModel];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:viewController animated:YES completion:nil];
}

#pragma mark - 构造方法

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    
    return self;
}

- (void)setUpUI
{
    // 添加子控件
    [self addSubview:self.imageView];
    [self addSubview:self.isGifImageView];
    [self addSubview:self.seeBigPictureButton];
    
    // 添加手势
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageViewAction:)]];
    
    
    // 设置自动布局
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.seeBigPictureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
    }];
    
    [self.isGifImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self);
    }];
}

#pragma mark - 懒加载

- (UIImageView *)imageView
{
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
        _imageView.userInteractionEnabled = YES;
        _imageView.contentMode = UIViewContentModeTop;
        _imageView.clipsToBounds = YES;
    }
    
    return _imageView;
}

- (UIImageView *)isGifImageView
{
    if (_isGifImageView == nil) {
        _isGifImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common-gif"]];
    }
    
    return _isGifImageView;
}

- (UIButton *)seeBigPictureButton
{
    if (_seeBigPictureButton == nil) {
        _seeBigPictureButton = [UIButton buttonWithTitle:@"点击查看大图" fontSize:15 titleColor:[UIColor whiteColor] backgroundImageName:@"see-big-picture-background" highlightedBackgroundImageName:@"see-big-picture-background" target:nil action:nil];
        [_seeBigPictureButton setImage:[UIImage imageNamed:@"see-big-picture"] forState:UIControlStateNormal];
        _seeBigPictureButton.titleEdgeInsets = UIEdgeInsetsMake(0, kMargin, 0, 0);
        _seeBigPictureButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, kMargin);
        _seeBigPictureButton.userInteractionEnabled = NO;
    }
    
    return _seeBigPictureButton;
}

@end
